<template>
  <div class="dashboard-container" >
    <div class="dashboard-text">
      房间类型管理
    </div>
    <div style="margin-left: 60%">
      <el-button type="primary" @click="addButton"
                 style="margin-left: 40px" v-if="this.roleName == '管理员'">新增
      </el-button>
      <el-input v-model="roomTypeName" placeholder="房间名称"
                style="width: 200px;margin-left: 40px" clearable/>

      <el-button type="primary" @click="selectAll"
                 style="margin-left: 40px">查询
      </el-button>
      <el-button style="margin-left: 40px" @click="check">重置</el-button>
    </div>
    <div style="margin-top: 20px">
      <el-table :data="roomTypeList" height="659px" border style="width: 100%">
        <el-table-column align="center" fixed prop="roomTypeId" sortable label="房间类型ID"/>
        <el-table-column align="center" fixed prop="roomTypeName" sortable label="房间类型名称"/>
        <el-table-column align="center" fixed prop="roomPrice" sortable label="房间价格"/>
        <el-table-column align="center" fixed prop="remark" sortable label="备注"/>
        <el-table-column align="center" fixed="right" label="操作" v-if="this.roleName == '管理员'">
          <template  v-slot="scope">
            <!--传入的scope.row参数为当前行的数据-->
            <el-button @click="updateButton(scope.row)" type="text" size="small">修改</el-button>
            <el-divider direction="vertical"></el-divider>
            <el-button  @click="deleteButton(scope.row)" type="text" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div>
      <!--新增弹窗-->
      <el-dialog title="新增" width="40%" :visible.sync="addButtonController" @close="close('ruleForm')">
        <div style="margin: 10px">
          <el-form label-width="20%" :rules="Roles" :model="addList" ref="ruleForm">
            <el-form-item label="房间名称"  prop="roomTypeName">
              <el-input v-model="addList.roomTypeName" maxlength="20" show-word-limit style="width: 80%"
                        clearable placeholder="请输入房间名称"/>
            </el-form-item>
            <el-form-item label="房间价格"  prop="roomPrice">
              <el-input type="number" v-model="addList.roomPrice" maxlength="20" show-word-limit style="width: 80%"
                        clearable placeholder="请输入房间价格"/>
            </el-form-item>
            <el-form-item label="备注"  prop="remark">
              <el-input v-model="addList.remark" maxlength="20" show-word-limit style="width: 80%"
                        clearable placeholder="请输入备注"/>
            </el-form-item>
          </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="add('ruleForm')">确定</el-button>
        <el-button @click="close('ruleForm')">取消</el-button>
      </span>
      </el-dialog>

      <!--修改弹窗-->
      <el-dialog title="修改" width="40%" :visible.sync="updateController" @close="close('ruleForm')">
        <div style="margin: 10px">
          <el-form label-width="20%" :rules="Roles" :model="updateList" ref="ruleForm">
            <el-form-item label="房间名称"  prop="roomTypeName">
              <el-input v-model="updateList.roomTypeName" maxlength="20" show-word-limit style="width: 80%"
                        clearable placeholder="请输入房间名称"/>
            </el-form-item>
            <el-form-item label="房间价格"  prop="roomPrice">
              <el-input type="number" v-model="updateList.roomPrice" maxlength="20" show-word-limit style="width: 80%"
                        clearable placeholder="请输入房间价格"/>
            </el-form-item>
            <el-form-item label="备注"  prop="remark">
              <el-input v-model="updateList.remark" maxlength="20" show-word-limit style="width: 80%"
                        clearable placeholder="请输入备注"/>
            </el-form-item>
          </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="update('ruleForm')">确定</el-button>
        <el-button @click="close('ruleForm')">取消</el-button>
      </span>
      </el-dialog>

      <!--删除弹窗-->
      <el-dialog title="删除" width="40%" :visible.sync="deleteController">
        <div style="margin: 10px">
          <span>请确认是否删除该房间类型信息</span>
        </div>
        <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="deleteRoomType">确定</el-button>
        <el-button @click="deleteController = false">取消</el-button>
      </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { deleteByUserId, insertByUser, selectAllUser, updateByUser } from '@/api/user'
import { deleteByRoomType, insertByRoomType, selectAllRoomType, updateByRoomType } from '@/api/roomType'

export default {
  name: 'roomType',
  computed: {
    ...mapGetters([
      'userId',
      'userName',
      'roleName'
    ])
  },
  // 页面参数
  data() {
    return {
      userId: this.$store.getters.userId,
      userName: this.$store.getters.userName,
      roleName: this.$store.getters.roleName,
      roomTypeName: '',
      roomTypeList: [],
      addButtonController: false,
      addList: {
        roomTypeName: '',
        roomPrice:'',
        remark: ''
      },
      Roles: {
        roomTypeName:[
          {required: true, trigger: 'blur', message: '房间名不能为空'},
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        ],
        roomPrice:[
          {required: true, trigger: 'blur', message: '房间价格不能为空且为数字'},
        ]
      },
      updateController: false,
      updateList: {
        roomTypeTableId: '',
        roomTypeId: '',
        roomTypeName: '',
        roomPrice: '',
        remark: ''
      },
      deleteController:false,
      deleteRoomTypeId:'',
    }
  },
  // 页面初始化加载方法
  mounted() {
    this.selectAll()
  },
  // 页面内事件和方法
  methods: {
    selectAll() {
      selectAllRoomType({ roomTypeName: this.roomTypeName }).then(
        response => {
          this.roomTypeList = response.data
        }
      )
    },
    check() {
      this.roomTypeName = ''
      this.selectAll()
    },
    addButton() {
      this.addList = {
        roomTypeName: '',
        roomPrice: '',
        remark: ''
      }
      this.addButtonController = true
    },
    add(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          insertByRoomType({
            roomTypeName: this.addList.roomTypeName,
            roomPrice: this.addList.roomPrice,
            remark: this.addList.remark,
          }).then(
            async response => {
              this.selectAll()
              this.addButtonController = false
            }
          )
        } else {
          return false;
        }
      });
    },
    close(formName){
      this.$refs[formName].resetFields();
      this.addButtonController =false
      this.updateController = false
    },
    updateButton(row) {
      this.updateList = {
        roomTypeTableId: row.roomTypeTableId,
        roomTypeId: row.roomTypeId,
        roomTypeName: row.roomTypeName,
        roomPrice: row.roomPrice,
        remark: row.remark,
      }
      this.updateController = true
    },
    update(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          updateByRoomType({
            roomTypeTableId: this.updateList.roomTypeTableId,
            roomTypeId: this.updateList.roomTypeId,
            roomTypeName: this.updateList.roomTypeName,
            roomPrice: this.updateList.roomPrice,
            remark: this.updateList.remark,
          }).then(
            async response => {
              this.selectAll()
              this.updateController = false
            }
          )
        } else {
          return false;
        }
      });
    },
    deleteButton(row){
      this.deleteRoomTypeId = row.roomTypeId
      this.deleteController = true
    },
    deleteRoomType() {
      deleteByRoomType({
        roomTypeId: this.deleteRoomTypeId,
      }).then(
        async response => {
          this.selectAll()
          this.deleteController = false
        }
      )
    },
  }
}
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }

  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
</style>
